import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'
import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart} from '../apis/core-util'

export default class MainScope extends Scope {
  constructor(props, context){
    super(props, context);
    this.viewType = 'domain';
    this.$state = {
      chart0:{
        loading:true
      },
      chart1:{
        loading:true
      },
      grid:{
        loading:true
      }
    }
    this.chart0Api = coreApi({
      limit:5,
      urlName:'errorrate',
      viewKey:'error_rate',
      order:sortMap['error_rate']
    },TimeStore.getValue,function(){
      if(this.viewType==='domain'){
        return {
          classify:'domain',
          statusCode:'400'
        }
      }else if(this.viewType==='statuscode'){
        return {
          classify:'domain',
          statusCode:'400'
        }
      }else if(this.viewType==='errorcode'){
        return {
          classify:'errorcode',
          errorCode:'0'
        }
      }
    }.bind(this));
    this.chart1Api = coreApi({
      limit:5,
      urlName:'errorrate',
      classify:'domain',
      viewKey:'error_rate',
      order:sortMap['error_rate']
    },TimeStore.getValue,function(){
      if(this.viewType==='domain'){
        return {
          classify:'domain',
          errorCode:'0'
        }
      }else if(this.viewType==='statuscode'){
        return {
          classify:'statuscode',
          statusCode:'400'
        }
      }else if(this.viewType==='errorcode'){
        return {
          classify:'domain',
          errorCode:'0'
        }
      }
    }.bind(this));
    this.gridApi = coreApi({
      urlName:'httperrorsimilar',
      size:1,
      limit:200,
      order:'101'
    },TimeStore.getValue);
  }
  fetch(){
    var _this = this
    this.chart0Api.fetch().then(function(data,params){
      _this.$state.chart0 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart0 = {
        error:err
      }
    },function(params){
      _this.$state.chart0 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart1Api.fetch().then(function(data,params){
      _this.$state.chart1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart1 = {
        error:err
      }
    },function(params){
      _this.$state.chart1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.fetchGrid()
  }
  fetchGrid(){
    var _this = this
    this.gridApi.fetch().then(function gridHandle(data,params){
      _this.$state.grid = {
        data:_.map(data.result,function(errorType){
          return _.extend({},errorType.key,errorType.combData)
        })
      }
    },function(err){
      _this.$state.grid = {
        error:err
      }
    },function(params){
      _this.$state.grid = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $changesort(payload){
    this.viewType = payload.key;
    this.fetch()
  }
  $settime(payload){
    this.fetch()
  }
  $load(){
    this.fetch()
  }
}
